package com.winit.openapi.fna.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.SPIException;
import com.winit.fna.spi.fundAccount.CustomerAccountService;
import com.winit.fna.spi.fundAccount.command.GetCustomerAccountByPartenrIdAndIsoCodeCommand;
import com.winit.fna.spi.fundAccount.vo.CustomerAccount;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;

/**
 * 资金賬戶
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * hui.li2 	1.0  		2015-4-7 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping("/fna/customerAccount")
public class CustomerAccountController extends BaseController {

    @Resource
    private CustomerAccountService customerAccountService;

    /**
     * 根据ID获取资金账户余额
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     * @author LH
     * @date 2015-4-7
     */
    @RequestMapping(value = "/getCustomerAccount")
    public String getCustomerAccount(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        GetCustomerAccountByPartenrIdAndIsoCodeCommand command = new GetCustomerAccountByPartenrIdAndIsoCodeCommand();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            try {
                command = JSONObject.toJavaObject(json, GetCustomerAccountByPartenrIdAndIsoCodeCommand.class);
            } catch (SPIException e) {
                throw new SPIException("javaBean 转换错误");
            }
            CustomerAccount customerAccount = customerAccountService.getCustomerAccount(command);
            if (null != customerAccount) {
                Map<String, Object> data = new HashMap<String, Object>();
                data.put("usableBalance", customerAccount.getUsableBalance());
                responseMsg.setData(data);
            }
        } else {
            throw new SPIException("json 格式错误");
        }
        return SUCCESS;
    }
}
